function [f , df]=gmmNL(thetaNL,BLPdata)
% gmmNL - This function is used to calculate the GMM estimator for the
% Nested Logit function
% 
% [f df]=gmmNL(thetaNL,BLPdata)
%
% Inputs:
%    input1 - Non linear parameters nested logit
%    input2 - Data
%
% Outputs:
%    f = gmm function value; df = gradient of f 
%
% Subfunctions: deltaLogit(theta,BLPdata) ; jacobLogit
%

% Author: Laura Grigolon and Frank Verboven
% August 2012;

%% Contraction Mapping
[d,iterat]   = deltaNL(thetaNL,BLPdata);

%% Contraction Mapping
if max(isnan(d)) == 1 || iterat>2500 || isreal(d)==0
 f  = 1e10;
else
%% GMM
if max(isnan(d)) == 1
 f  = 1e10;	  
else 
% Use relationship between linear and non-linear parameters from step 4,
% resulting from the FOC's
    betaNL  = BLPdata.invxzwzx*(BLPdata.xzwz*d);
    % error term
    csi     = d - BLPdata.Xexo*betaNL;
	f       = csi'*BLPdata.Z*BLPdata.W*BLPdata.Z'*csi;
    save betaNL betaNL;
end
%% Gradient
if max(isnan(d)) == 1 || iterat>2500 || isreal(d)==0    
    % isreal(d)==0 can happen because of the negative weights - this is a problem of quadrature methods
    df=1e10;
else
        temp    = jacobNL(thetaNL,d,BLPdata);
        df      = 2*temp'*BLPdata.Z*BLPdata.W*BLPdata.Z'*csi;
end

end

